package com.ysxsoft.wdg.ui.activity.shopcart;

import com.ysxsoft.wdg.bean.cart.ShopcartBean;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class ShopcartHelper {

    //是否有选中项
    public static boolean hasSelect(List<ShopcartBean> list) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isSelect()) {
                return true;
            }
        }
        return false;
    }


    //计算选中总价格
    public static Double getTotal(List<ShopcartBean> list) {
        double sum = 0;
        for (int j = 0; j < list.size(); j++) {
            if (list.get(j).isSelect()) {
                double price = Double.parseDouble(list.get(j).getL_price());
                sum += price * list.get(j).getNum();
            }
        }
        return sum;
    }

    //是否全选
    public static boolean isAllSelect(List<ShopcartBean> list) {
        if (list == null || list.size() == 0)
            return false;
        boolean isSelect = true;
        for (int i = 0; i < list.size(); i++) {
            if (!list.get(i).isSelect()) {
                isSelect = false;
            }
        }
        return isSelect;
    }


    //选中的总数
    public static int getSelectNum(List<ShopcartBean> list) {
        int num = 0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isSelect()) {
                num += list.get(i).getNum();
            }
        }
        return num;
    }


    //获取 删除选中项
    public static List<Integer> getSelectCarIds(List<ShopcartBean> list) {
        List<Integer> needRemoveList = new ArrayList<>();

        Iterator<ShopcartBean> it = list.iterator();
        while (it.hasNext()) {
            ShopcartBean next = it.next();
            if (next.isSelect()) {
                needRemoveList.add(Integer.parseInt(next.getCarid()));
            }
        }
        return needRemoveList;
    }

    //删除选中项
    public static void deleteSelectItem(List<ShopcartBean> list) {
        Iterator<ShopcartBean> it = list.iterator();
        while (it.hasNext()) {
            ShopcartBean next = it.next();
            if (next.isSelect()) {
                it.remove();
            }
        }
    }

    //选中状态
    public static void setSelect(List<ShopcartBean> list, boolean isSelect, boolean isChangeEdit) {
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setSelect(isSelect);
            if (isChangeEdit)
                list.get(i).setEdit(isSelect);
        }
    }

}
